﻿CREATE PROCEDURE [acms].[PostTopic_Put]
	@PostId int, 
	@TopicId int,
	@VersionId timestamp,
	@PostNameOut varchar(100) output,
	@PostDisplayNameOut varchar(100) output,
	@TopicNameOut varchar(100) output,
	@VersionIdOut timestamp output
AS
	if not Exists(select null from PostTopics where PostId = @PostId and TopicId = @TopicID) begin
		insert into PostTopics
			(PostId, TopicId)
		values
			(@PostId, @TopicId)
	end
	else if exists (select null from PostTopics where PostId = @PostId and TopicId = @TopicID and Deleted = 1) begin
		update PostTopics
		set Deleted = 0
		where PostId = @PostId and TopicId = @TopicID
	end
	select @PostNameOut = PostName, @PostDisplayNameOut = PostDisplayName, @TopicNameOut = TopicName, @VersionIdOut = VersionId 
	from PostTopic_View where PostId = @PostId and TopicId = @TopicID

RETURN 0;